﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Data = DeltaFlex.Data;
using System.Data;
using Taula = DeltaFlex.Negoci.ConsTaules.Articles;

namespace DeltaFlex.Negoci
{
    [Serializable()]
    public class Articles
    {
        #region Variables
        private int _idArticle;
        private string _referenciaArticle;
        private string _nomArticle;
        private string _unitat;
        private decimal _pvp;
        #endregion

        #region Atributs
        public int IdArticle
        {
            get { return _idArticle; }
        }

        public string ReferenciaArticle
        {
            get { return _referenciaArticle; }
            set { _referenciaArticle = value; }
        }

        public string NomArticle
        {
            get { return _nomArticle; }
            set { _nomArticle = value; }
        }

        public string Unitat
        {
            get { return _unitat; }
            set { _unitat = value; }
        }

        public decimal Pvp
        {
            get { return _pvp; }
            set { _pvp = value; }
        }
        #endregion

        public Articles()
        { }

        #region Inserts
        public static int InsertarArticle(string referencia, string nom, string unitats, decimal pvp)
        {
            try
            {
                return Data.Article.InsertarArticle(referencia, nom, unitats, pvp);
            }
            catch { throw; }
        }
            
        #endregion

        #region Selects
        public static List<Articles> LlistaTotsArticles()
        {
            List<Articles> lstArticles = new List<Articles>();
            IDataReader reader = null;

            lstArticles.Add(new Articles());
            try
            {
                reader = Data.Article.LlistaTotsArticles();

                while (reader.Read())
                {
                    lstArticles.Add(MapEntityFromDataReader(reader));
                }
            }
            catch { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }

            return lstArticles;
        }

        public static Articles CercarArticleByIdArticle(int idArticle)
        {
            Articles article = new Articles();
            IDataReader reader = null;

            try
            {
                reader = Data.Article.LlistarArticleByIdArticle(idArticle);

                if (reader.Read())
                    article = MapEntityFromDataReader(reader);
            }
            catch { throw; }
            finally
            {
                if (reader != null) reader.Close();
            }

            return article;
        }
        
        #endregion

        #region Mapeig
        public static Articles MapEntityFromDataReader(IDataReader record)
        {
            Articles article = new Articles();

            MemberAllocation.Allocate(record, Taula.COLUMNA_IDARTICLE, ref  article._idArticle);
            MemberAllocation.Allocate(record, Taula.COLUMNA_REFERENCIAARTICLE, ref  article._referenciaArticle);
            MemberAllocation.Allocate(record, Taula.COLUMNA_NOMARTICLE, ref  article._nomArticle);
            MemberAllocation.Allocate(record, Taula.COLUMNA_UNITAT, ref  article._unitat);
            MemberAllocation.Allocate(record, Taula.COLUMNA_PVP, ref  article._pvp);

            return article;
        }

        public static Articles MapEntityFromId(IDataReader record)
        {
            Articles article = new Articles();

            MemberAllocation.Allocate(record, Taula.COLUMNA_IDARTICLE, ref article._idArticle);

            return CercarArticleByIdArticle(article._idArticle);
        }

        #endregion
    }
}
